package com.yaoxun.console.config;

import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MultipartException;

import com.fasterxml.jackson.core.JsonParseException;
import com.yaoxun.console.code.BaseResponseCode;
import com.yaoxun.console.exception.JsonException;
import com.yaoxun.console.exception.ValidatorException;
import com.yaoxun.console.xo.resp.ApiResp;

import lombok.extern.slf4j.Slf4j;

/**
 * 全局异常处理
 * @author Loren
 * @Datetime 2021年7月10日 下午11:40:03
 */
@Slf4j
@RestControllerAdvice
public class ExceptionConfig {

	
	@ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ApiResp<Object> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
		BindingResult bindingResult = e.getBindingResult();
        FieldError fieldError = bindingResult.getFieldError();
		return ApiResp.of(BaseResponseCode.PARAM_ERROR.code(), fieldError.getDefaultMessage());
    }

    @ExceptionHandler(value = ValidatorException.class)
    public ApiResp<Object> handleValidatorException(ValidatorException e) {
    	
    	String code = null;
    	
    	if(e.getResponseCode() == null) {
    		code = BaseResponseCode.PARAM_ERROR.code();
    	} else {
    		code = e.getResponseCode().code();
    	}
    	
        return ApiResp.of(code, e.getMessage());
    }
    
    @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
    public ApiResp<Object> handleValidationException(HttpRequestMethodNotSupportedException e) {
    	return ApiResp.of(BaseResponseCode.NOT_SUPORT_HTTP_METHOD);
    }
    
    @ExceptionHandler(value = HttpMediaTypeNotSupportedException.class)
    public ApiResp<Object> handleMediaTypeNotSupportedException(HttpMediaTypeNotSupportedException e) {
    	log.error("request not suport media type", e);
        return ApiResp.of(BaseResponseCode.NOT_SUPORT_MEDIA_TYPE);
    }

    @ExceptionHandler(value = HttpMessageNotReadableException.class)
    public ApiResp<Object> handleMessageNotReadableException(HttpMessageNotReadableException e) {
    	log.error("request param json error,", e);
        return ApiResp.of(BaseResponseCode.PARAM_ERROR.code(), "请求参数不符合JSON格式");
    }

    @ExceptionHandler(value = JsonParseException.class)
    public ApiResp<Object> handleJsonParseException(JsonParseException e) {
    	log.error("request param json error,", e);
        return ApiResp.of(BaseResponseCode.PARAM_ERROR.code(), "请求参数不符合JSON格式");
    }
    
    @ExceptionHandler(value = JsonException.class)
    public ApiResp<Object> handleJsonException(JsonException e) {
    	log.error("request param json error,", e);
    	return ApiResp.of(BaseResponseCode.PARAM_ERROR.code(), "请求参数不符合JSON格式");
    }

    @ExceptionHandler(value = MultipartException.class)
    public ApiResp<Object> handleMultipartException(MultipartException e) {
    	log.error("request param multipart error", e);
        return ApiResp.of(BaseResponseCode.PARAM_ERROR.code(), "请求参数文件上传错误");
    }

    /**
     * 拦截所有的异常
     * 
     * @author Loren
     * @Datetime 2021年5月13日 下午4:53:54
     * @param ex
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public ApiResp<Object> handleException(Exception e) {
    	log.error("system error:", e);
        return ApiResp.of(BaseResponseCode.ERROR_UNKNOW);
    }
	
}
